package bloggerdesktop;

import com.google.gdata.data.*;
import com.google.gdata.data.extensions.*;
import com.google.gdata.util.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class TextViewer extends JFrame {
    public static CommentViewer cv;
    private JEditorPane html;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    GoogleDataHandler gdh;
    String blog_id;
    String post_id;
    public static String[][] inter;
    public void ViewComments(java.awt.event.ActionEvent evt)
    {
        try {

            inter = gdh.printAllComments(blog_id, post_id);
        } catch (ServiceException ex) {
            Logger.getLogger(TextViewer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TextViewer.class.getName()).log(Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                cv = new CommentViewer(inter);
                cv.setVisible(true);
            }
        });
    }
    public void AddComment(java.awt.event.ActionEvent evt)
    {
        
    }

    public TextViewer(Entry ent, GoogleDataHandler gdhi, String blog_idi, String post_idi) {
        gdh = gdhi;
        blog_id = blog_idi;
        post_id = post_idi;
        
        final Desktop linkSrv = Desktop.getDesktop();  //Requires Java 6
        setTitle("Post Viewer");
        setSize(600,400);
        setBackground(Color.white);
        getContentPane().setLayout(new BorderLayout());
        JPanel topPanel = new JPanel();
        JButton jButton1 = new javax.swing.JButton();
        jButton1.setText("View Comments");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ViewComments(evt);
            }});
        JButton jButton2 = new javax.swing.JButton();
        jButton2.setText("Add Comment");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddComment(evt);
            }});
            
            
            
        try {
            
            html = new JEditorPane("text/html",retrieveHtml(ent));
            html.setEditable(false);
            html.addHyperlinkListener(new HyperlinkListener() {
                public void hyperlinkUpdate(HyperlinkEvent e) {
                    try{
                        if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
                            linkSrv.browse(URI.create(e.getURL().toString()));
                    }
                    catch (IOException ee){
                        JOptionPane.showMessageDialog(null,"Error accessing the browser.");
                    }
                }
            });
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.getViewport().add(html, BorderLayout.CENTER);
            
            
            jPanel1 = new javax.swing.JPanel();

        //setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(163, 163, 163)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addContainerGap(190, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)))
        );

        pack();
        

            scrollPane.updateUI();

        } catch (MalformedURLException e) {
            System.out.println("Malformed URL: " + e);
        } catch (IOException e) {
            System.out.println("IOException: " + e);
        }
    }

    private String retrieveHtml(Entry ent) throws MalformedURLException {
        TextContent t=(TextContent)ent.getContent();
        HtmlTextConstruct ht = (HtmlTextConstruct)t.getContent();
        return ht.getHtml();
    }
    
    
}